const http = require('http');
const https = require('https');
const path = require('path');
const fs = require('fs');
const util = require('util');

function readFileAsString(filePath) {
  const readFile = util.promisify(fs.readFile);
  return readFile(filePath).then(file => file.toString());
}

exports.createServer = async function createServer(app) {
  let server = null;
  if (!app.get('HTTP')) {
    const options = {
      key: await readFileAsString(path.join(__dirname, './private/server.key')),
      cert: await readFileAsString(path.join(__dirname, './private/server.ctr')),
    };
    server = https.createServer(options, app);
  } else {
    server = http.createServer(app);
  }
  return server;
};
